//登录注册模块
let LoginRegist = {

  //***********初始数据定义************/
  page: null,
  mta: null,
  app: null,
  api: null,
  path: null,
  alertList: {

    Phone: '手机号码不能为空',

    Phonecode: '验证码不能为空'
  },
  Arr: ['Phonecode', 'Phone'],

  //***********参数获取部分************/
  //传入外部参数
  addParam: function (that, app, api, path, mta) {
    this.page = that;
    this.app = app;
    this.api = api;
    this.path = path;
    this.mta = mta;
  },
  //获取输入
  bindInput: function (e) {
    let page = this.page;
    let id = e.currentTarget.id;

    page.setData({
      [id]: e.detail.value
    })
  },

  //***********登录注册部分************/
  //注册
  regist: function (suc) {
    let _this = this;
    let page = this.page;
    let app = this.app;
    let api = this.api;

    if (!_this.checkEmpty('Phone', 'Phonecode', 'Pwd')) return;

    let dataDic = {
      phone: page.data.Phone,
      Code: page.data.Phonecode,
      UserName: page.data.Name,
      UserPwd: page.data.Pwd,
      Source: app.globalData.appSource,
      SourceType: 2,
      OpenId: app.globalData.openId,
    };

    wx.showLoading({
      title: "加载中",
    })

    api.methodWechatRegister({
      dataDic,
      complete: function (res) {
        console.log(res);
        wx.hideLoading();
        // typeof suc == 'function' && suc();
        if (res.data.code != 0) {
          wx.showModal({
            title: '温馨提示',
            showCancel: false,
            content: res.data.msg,
          })
          return;
        } else {
          typeof suc == 'function' && suc();
          _this.nextLogin();
          setTimeout(() => {
            wx.showModal({
              title: '注册成功!',
              content: '我们的课程顾问将会通过电话联系您进行预约上课，请耐心等待',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }, 2000);
        }
      }
    })
  },
  //登录
  nextLogin: function (suc) {
    let _this = this;
    let page = this.page;
    let app = this.app;
    let api = this.api;
    let path = this.path;

    let dataDic = {
      UserPwd: page.data.Pwd,
      UserAccount: page.data.Phone,
      OpenId: app.globalData.openId,
    }

    api.methodWechatLogin({
      dataDic,
      success: (res) => {
        console.log(res);
        wx.hideLoading();
        if (res.data.code != 0) {
          wx.showModal({
            title: '温馨提示',
            showCancel: false,
            content: res.data.msg,
          })
          return;
        } else {
          //登录成功后，同步用户数据，返回个人中心界面
          app.globalData.loginStatus = true;
          wx.setStorage({
            key: 'loginStatus',
            data: true,
          })
          typeof suc == 'function' && suc();
          if (page.data.fromPage == "score") {
            console.log(getCurrentPages())
            let pagesArr = getCurrentPages()
            for (let i = 0; i < pagesArr.length; i++) {
              let pageItem = pagesArr[i]
              if (pageItem.route == path.scorePreview) {
                wx.navigateBack({
                  delta: pagesArr.length - i - 1
                })
                return
              }
            }
          } else {
            wx.switchTab({
              url: '/' + path.index
            })
          }
        }
      },
      fail: (res) => {
        console.log(res);
        wx.showModal({
          title: '温馨提示',
          showCancel: false,
          content: '自动登录失败，请返回登录界面重新登录！',
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/' + path.login + '?fromPage=' + page.data.fromPage,
              })
            }
          }
        });
      }
    });
  },

  //短信验证码登录
  login_Code: function (suc) {
    let _this = this;
    let page = this.page;
    let mta = this.mta;
    let app = this.app;
    let api = this.api;
    let path = this.path;

    if (!_this.checkEmpty('Phone', 'Phonecode', '')) return;

    wx.showLoading({
      title: '登录中...',
    })

    let dataDic = {
      TelePhone: page.data.Phone,
      Code: page.data.Phonecode,
      //OpenId: app.globalData.openId,
    };
    api.methodWechatLoginByVerificationCode({
      dataDic,
      success: (res) => {
        console.log(res);
        wx.hideLoading();
        if (res.data.code != 0) {
          wx.showModal({
            title: '温馨提示',
            showCancel: false,
            content: res.data.msg,
          })
          return;
        } else {
          console.log('登录成功返回的数据', res.data);

          app.globalData.UID = res.data.data.UID;
          //登录成功后，同步用户数据，返回个人中心界面
          app.globalData.loginStatus = true;
          app.globalData.loginPhone = page.data.Phone,
            wx.setStorage({
              key: 'loginStatus',
              data: true,
            })
          wx.setStorage({
            key: 'loginPhone',
            data: page.data.Phone,
          })
          wx.setStorage({
            key: 'UID',
            data: res.data.data.UID,
          })
          typeof suc == 'function' && suc();
          if (page.data.fromPage == "index") {
            console.log("这个是界面的数据", getCurrentPages())
            let pagesArr = getCurrentPages()
            for (let i = 0; i < pagesArr.length; i++) {
              let pageItem = pagesArr[i]
              if (pageItem.route == path.index) {
                console.log(pagesArr.length, i)
                wx.navigateBack({
                  delta: pagesArr.length - i - 1
                })
                return
              }
            }
          } else {
            wx.switchTab({
              url: '/' + path.my
            })
          }
        }
      },
      fail: (res) => {
        console.log(res);
        wx.showModal({
          title: '温馨提示',
          showCancel: false,
          content: '登录失败，请检查网络情况！',
        });
      }
    })
  },
  //验证数据是否为空
  checkEmpty: function (a, b, c) {
    let _this = this;
    let page = this.page;
    let flag = true

    if (a == 'Phone' && page.data['Phone'].length <= 0) {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: _this.alertList.Phone
      })
      flag = false;
      return false;
    }
    if (b == 'Phonecode' && page.data['Phonecode'].length <= 0) {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: _this.alertList.Phonecode
      })
      flag = false;
      return false;
    }
   
    return flag
  },

  //***********验证码部分************/
  //点击获取短信验证码
  tapGetAuthCode: function (e, category) {
    let _this = this;
    let page = this.page;

    if (page.data.Phone.length > 0) {
      if (category != 'login') {
        _this.getAuthCode();
      } else {
        _this.getAuthCode_login();
      }
    } else {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: '请填写正确的手机号',
      });
      return false;
    }
  },
  //注册获取验证码
  getAuthCode: function () {
    let _this = this;
    let page = this.page;
    let app = this.app;
    let api = this.api;
    let path = this.path;

    let dataDic = {
      phone: page.data.Phone,
    };
    api.methodWechatGetSMSCode({
      dataDic,
      success: (res) => {
        console.log(res);
        page.setData({
          verification: res.data.data.code
        })
        if (page.data.verification == -9) {
          console.log("不调用60秒倒计时函数")
        } else if (page.data.verification == 0) {
          _this.countdownTime();
        }
        console.log(page.data.verification)
        if (res.data.code != 0) {
          wx.showModal({
            title: '温馨提示',
            showCancel: false,
            content: res.data.msg,
          });
          return;
        } else {
          if (res.data.data.code != 0) {
            wx.showModal({
              title: '温馨提示',
              showCancel: false,
              content: res.data.data.msg,
            })
          } else {
            wx.showToast({
              title: '发送成功',
            })
          }
        }
      },
      fail: (res) => {
        console.log(res);
        wx.showModal({
          title: "温馨提示",
          showCancel: false,
          content: "获取验证码失败，请稍后重试！",
        });
      }
    });
  },
  //登录授权获取验证码
  getAuthCode_login: function () {
    let _this = this;
    let page = this.page;
    let app = this.app;
    let api = this.api;
    let path = this.path;

    let dataDic = {
      TelePhone: page.data.Phone,
   
    };
    api.methodWechatGetVerificationCode({
      dataDic,
      success: (res) => {
        console.log(res);
        page.setData({
          verification: res.data.code
        })
        if (page.data.verification == -4) {
          console.log("+++")
        } else if (page.data.verification == 0) {
          console.log("----")
          _this.countdownTime();
        }
        console.log(page.data.verification)
        console.log(res.data.code)
        if (res.data.code != 0) {
          if (res.data.code == -7) {
            wx.showModal({
              title: '温馨提示',
              showCancel: false,
              content: res.data.msg,
              success: function (res) {
                console.log(res)
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            })
          } else if (res.data.code == -4) {
            wx.showModal({
              title: '温馨提示',
              showCancel: true,
              confirmText: "去注册",
              content: res.data.msg,
              success: function (res) {
                console.log("测试弹框")
                console.log(res)
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/' + path.regist
                  })
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            });
          }
          return;
        } else {
          if (res.data.code != 0) {
            wx.showModal({
              title: '温馨提示',
              showCancel: true,
              content: res.data.msg,
              success: function (res) {
                console.log(res)
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx.showToast({
              title: '发送成功',
            })
          }
        }
      },
      fail: (res) => {
        console.log(res);
        wx.showModal({
          title: "温馨提示",
          showCancel: false,
          content: "获取验证码失败，请稍后重试！",
        });
      }
    });
  },
  //倒计时
  countdownTime: function () {
    let _this = this;
    let page = this.page;
    let initTime = 60;

    page.setData({
      getCode_ing: true,
      remainTime: initTime
    })

    page.downTimer = setInterval(function () {
      initTime--;
      page.setData({
        remainTime: initTime
      })
      if (initTime <= 0) {
        clearInterval(page.downTimer);
        page.setData({
          getCode_ing: false
        })
      }
    }, 1000);

  },
 
}

module.exports = LoginRegist